package com.itxiao.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itxiao.pojo.bo.Page;
import com.itxiao.pojo.bo.film.GetCinemaBo;
import com.itxiao.pojo.bo.film.GetFilmBo;
import com.itxiao.pojo.bo.film.GetLocationBo;
import com.itxiao.pojo.bo.film.GetShowBo;
import com.itxiao.pojo.bo.spu.AddSpuReq;
import com.itxiao.pojo.vo.R;
import com.itxiao.pojo.vo.film.QueryRespVo;
import com.itxiao.util.HttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("wx/api/v1/film")
@Api(tags = "电影模块")
@CrossOrigin
public class FilmController {
    @ApiOperation("查询全国城市")
    @PostMapping("/query/aree")
    public R queryArea() {
        Map<String, Object> params = new HashMap<>();
        long timestamp = System.currentTimeMillis();
        params.put("platformId", "10559");
        params.put("timestamp", String.valueOf(timestamp));
        String sign =  HttpUtil.generateSign(null,"10559",String.valueOf(timestamp),"e6hgoe7wi77lw0gq");
        params.put("sign", sign);
        String s = HttpUtil.postJson(HttpUtil.BASE_URL + HttpUtil.LIST_ALL_CITIES, JSONUtil.toJsonStr(params));
        JSONObject entries = JSONUtil.parseObj(s);
        QueryRespVo vo  = new QueryRespVo();
        BeanUtil.copyProperties(entries,vo);
        return R.ok(vo.getData());
    }

    @ApiOperation("根据经纬度定位城市")
    @PostMapping("/get/location")
    public R GetLocation(@RequestBody GetLocationBo bo) {
        Map<String, Object> params = new HashMap<>();
        long timestamp = System.currentTimeMillis();
        params.put("platformId", "10559");
        params.put("lon", String.valueOf(bo.getLon()));
        params.put("lat", String.valueOf(bo.getLat()));
        params.put("timestamp", String.valueOf(timestamp));
        String sign =  HttpUtil.generateSign(params,"10559",String.valueOf(timestamp),"e6hgoe7wi77lw0gq");
        params.put("sign", sign);
        String s = HttpUtil.postJson(HttpUtil.BASE_URL + HttpUtil.LIST_REGIONS_BY_CITY_ID, JSONUtil.toJsonStr(params));
        Map<String, Object> responseMap = JSONUtil.toBean(s, Map.class, false);
        Object data = responseMap.get("data");
        return R.ok(data);
    }
    @ApiOperation("查询电影院列表")
    @PostMapping("/get/cinema")
    public R GetCinema(@RequestBody GetCinemaBo bo) {
        Map<String, Object> params = new HashMap<>();
        long timestamp = System.currentTimeMillis();
        params.put("platformId", "10559");
        params.put("lon", String.valueOf(bo.getLon()));
        params.put("lat", String.valueOf(bo.getLat()));
        params.put("cityId", String.valueOf(bo.getCityId()));
        params.put("showDate", bo.getShowDate());
        params.put("brandKeyword", bo.getBrandKeyword());
        params.put("filmId", String.valueOf(bo.getFilmId()));
        params.put("pageSize", String.valueOf(bo.getPageSize()));
        params.put("pageIndex", String.valueOf(bo.getPageIndex()));
        params.put("timestamp", String.valueOf(timestamp));
        String sign =  HttpUtil.generateSign(params,"10559",String.valueOf(timestamp),"e6hgoe7wi77lw0gq");
        params.put("sign", sign);
        String s = HttpUtil.postJson(HttpUtil.BASE_URL + HttpUtil.GET_CINEMA, JSONUtil.toJsonStr(params));
        Map<String, Object> responseMap = JSONUtil.toBean(s, Map.class, false);
        Object data = responseMap.get("data");
        return R.ok(data);
    }
    @ApiOperation("查询电影列表")
    @PostMapping("/get/film")
    public R GetFilm(@RequestBody GetFilmBo bo) {
        Map<String, Object> params = new HashMap<>();
        long timestamp = System.currentTimeMillis();
        params.put("platformId", "10559");
        params.put("cityId", String.valueOf(bo.getCityId()));
        params.put("cinemaId", String.valueOf(bo.getCinemaId()));
        params.put("pageIndex", String.valueOf(bo.getPageIndex()));
        params.put("pageSize", String.valueOf(bo.getPageSize()));
        params.put("timestamp", String.valueOf(timestamp));
        String sign =  HttpUtil.generateSign(params,"10559",String.valueOf(timestamp),"e6hgoe7wi77lw0gq");
        params.put("sign", sign);
        String s = HttpUtil.postJson(HttpUtil.BASE_URL + HttpUtil.GET_FILM, JSONUtil.toJsonStr(params));
        Map<String, Object> responseMap = JSONUtil.toBean(s, Map.class, false);
        Object data = responseMap.get("data");
        return R.ok(data);
    }
    @ApiOperation("查询电影详情")
    @GetMapping("/get/film/detail")
    public R GetFilmDetail(@RequestParam Long filmId) {
        Map<String, Object> params = new HashMap<>();
        long timestamp = System.currentTimeMillis();
        params.put("platformId", "10559");
        params.put("filmId", String.valueOf(filmId));
        params.put("timestamp", String.valueOf(timestamp));
        String sign =  HttpUtil.generateSign(params,"10559",String.valueOf(timestamp),"e6hgoe7wi77lw0gq");
        params.put("sign", sign);
        String s = HttpUtil.postJson(HttpUtil.BASE_URL + HttpUtil.GET_FILM_DETAIL, JSONUtil.toJsonStr(params));
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> responseMap = objectMapper.readValue(s, new TypeReference<Map<String, Object>>() {});
            return R.ok(responseMap.get("data"));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
    @ApiOperation("查询电影场次列表")
    @PostMapping("/get/show")
    public R GetShow(@RequestBody GetShowBo bo) {
        Map<String, Object> params = new HashMap<>();
        long timestamp = System.currentTimeMillis();
        params.put("platformId", "10559");
        params.put("cinemaId", String.valueOf(bo.getCinemaId()));
        params.put("filmId", String.valueOf(bo.getFilmId()));
        params.put("timestamp", String.valueOf(timestamp));
        String sign =  HttpUtil.generateSign(params,"10559",String.valueOf(timestamp),"e6hgoe7wi77lw0gq");
        params.put("sign", sign);
        String s = HttpUtil.postJson(HttpUtil.BASE_URL + HttpUtil.GET_SHOW, JSONUtil.toJsonStr(params));
        Map<String, Object> responseMap = JSONUtil.toBean(s, Map.class, false);
        Object data = responseMap.get("data");
        return R.ok(data);
    }
    @ApiOperation("查询电影座位列表")
    @GetMapping("/get/seat")
    public R GetLShowSeat(@RequestParam Long showId) {
        Map<String, Object> params = new HashMap<>();
        long timestamp = System.currentTimeMillis();
        params.put("platformId", "10559");
        params.put("showId", String.valueOf(showId));
        params.put("timestamp", String.valueOf(timestamp));
        String sign =  HttpUtil.generateSign(params,"10559",String.valueOf(timestamp),"e6hgoe7wi77lw0gq");
        params.put("sign", sign);
        String s = HttpUtil.postJson(HttpUtil.BASE_URL + HttpUtil.GET_SHOW_SEAT, JSONUtil.toJsonStr(params));
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> responseMap = objectMapper.readValue(s, new TypeReference<Map<String, Object>>() {});
            return R.ok(responseMap.get("data"));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
